package org.example.forkjoin;

import java.util.concurrent.RecursiveTask;

public class myForkJoin extends RecursiveTask<Long> {
    private Long start;
    private Long end;

    private Long temp = 10000L;

    public myForkJoin(Long start, Long end) {
        this.start = start;
        this.end = end;
    }

    @Override
    protected Long compute() {
        Long sum = 0L;
        if (end - start <= temp){
            for (Long i = start; i < end; i++) {
                    sum += i;
            }
            return sum;
        }else {
            // 定义一个中间值
            Long middle = (start + end) / 2;
            myForkJoin myForkJoin = new myForkJoin(start,middle );
                myForkJoin.fork();
            myForkJoin myForkJoin1 = new myForkJoin(middle+1,end );
            myForkJoin1.fork();
          return   myForkJoin.join()+ myForkJoin1.join();

        }
    }
}
